package com.allwees.bs.c.module.order.mq.listener;

import com.allwees.bs.c.module.order.entity.OrderEntity;
import com.allwees.bs.c.module.order.mapper.OrderMapper;
import com.allwees.bs.c.module.order.repository.OrderRepository;
import com.allwees.bs.c.module.payment.req.PaymentCaptureReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.RabbitListenerErrorHandler;
import org.springframework.amqp.rabbit.support.ListenerExecutionFailedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class PayMqErrorHandler implements RabbitListenerErrorHandler {

    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private OrderMapper orderMapper;

//    @Autowired
//    private OrderLogActionUtil orderLogActionUtil;

    @Override
    public Object handleError(Message amqpMessage, org.springframework.messaging.Message<?> message, ListenerExecutionFailedException exception) throws Exception {
        log.error(exception.getMessage(), exception);

        PaymentCaptureReq captureReq = (PaymentCaptureReq) message.getPayload();
        OrderEntity order = orderRepository.getByUuid(captureReq.getOrderUuid());
//        orderLogActionUtil.logAction(orderMapper.toDto(order),EActionType.ORDER_PAY_FAILED);
        return exception;
    }
}
